package spring.resources;

import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

public class DomElementHolder {

    private static final ThreadLocal<Map<String, Element>> THREAD_LOCAL = new ThreadLocal<>();

    private DomElementHolder() {
        init();
    }

    private static void init() {
        THREAD_LOCAL.set(new HashMap<>(20));
    }

    public static Element getElement(String id) {
        return THREAD_LOCAL.get().get(id);
    }

    public static void setElement(String id, Element element) {
        THREAD_LOCAL.get().put(id, element);
    }

    public static boolean containId(String id) {
        return THREAD_LOCAL.get().containsKey(id);
    }

    public static boolean containElement(Element element) {
        return THREAD_LOCAL.get().containsValue(element);
    }

    public static void destroy() {
        THREAD_LOCAL.remove();
    }

    public static void refreshElement(Map<String, Element> map) {
        THREAD_LOCAL.set(map);
    }
}
